//! грани куба обращенные внутрь для GL_TRIANGLE_STRIP
const float SKYBOX_DATA[120] = {
	-1, 1, 1, 0, 0, -1, 1,-1, 0, 1,  1, 1, 1, 1, 0,  1, 1,-1, 1, 1, // top
	-1,-1, 1, 0, 0,	 1,-1, 1, 1, 0, -1,-1,-1, 0, 1,	 1,-1,-1, 1, 1, // bottom
	-1,-1,-1, 0, 0,	-1, 1,-1, 0, 1,	-1,-1, 1, 1, 0,	-1, 1, 1, 1, 1, // left
	 1,-1,-1, 1, 0,	 1,-1, 1, 0, 0,	 1, 1,-1, 1, 1,	 1, 1, 1, 0, 1, // right
	-1,-1,-1, 1, 0,	 1,-1,-1, 0, 0,	-1, 1,-1, 1, 1,	 1, 1,-1, 0, 1, // front
	-1,-1, 1, 0, 0,	-1, 1, 1, 0, 1,	 1,-1, 1, 1, 0,	 1, 1, 1, 1, 1, // back
};

class Skybox {

friend class Scene3D;

protected:

	GLuint _texture[6];

	void _draw(const mat4& skybox_matrix){
		if (enable) {
			glDisable(GL_DEPTH_TEST);

			glMatrixMode(GL_PROJECTION);
			glLoadMatrixf(value_ptr(skybox_matrix));

			glEnableClientState(GL_VERTEX_ARRAY);
			glVertexPointer(3,GL_FLOAT,sizeof(float)*5,SKYBOX_DATA);
			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
			glTexCoordPointer(2,GL_FLOAT,sizeof(float)*5,&SKYBOX_DATA[3]);

			glBindTexture(GL_TEXTURE_2D,_texture[0]);
			glDrawArrays(GL_TRIANGLE_STRIP,0,4);

			glBindTexture(GL_TEXTURE_2D,_texture[1]);
			glDrawArrays(GL_TRIANGLE_STRIP,4,4);

			glBindTexture(GL_TEXTURE_2D,_texture[2]);
			glDrawArrays(GL_TRIANGLE_STRIP,8,4);

			glBindTexture(GL_TEXTURE_2D,_texture[3]);
			glDrawArrays(GL_TRIANGLE_STRIP,12,4);

			glBindTexture(GL_TEXTURE_2D,_texture[4]);
			glDrawArrays(GL_TRIANGLE_STRIP,16,4);

			glBindTexture(GL_TEXTURE_2D,_texture[5]);
			glDrawArrays(GL_TRIANGLE_STRIP,20,4);

			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
			glDisableClientState(GL_VERTEX_ARRAY);
			
			glClear(GL_DEPTH_BUFFER_BIT);
			
			glEnable(GL_DEPTH_TEST);
			
		} else
			glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	}

public:

	bool enable;

	void load(const char* name){
		static const char* names[6] = {
			"top.tga",
			"bottom.tga",
			"left.tga",
			"right.tga",
			"front.tga",
			"back.tga"
		};
		for (int i=0;i!=6;i++){
			glBindTexture(GL_TEXTURE_2D,_texture[i]);
			if (glfwLoadTexture2D((string("data/skybox/")+name+"/"+names[i]).c_str(),0)){
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
			}
		}
	}

	Skybox(){
		glGenTextures(6,_texture);
		enable = false;
	}

	~Skybox(){
		glDeleteTextures(6,_texture);
	}
	
};